//
//  Common.m
//  Together
//
//  Created by Dawn on 12-12-15.
//  Copyright (c) 2012年 Dawn. All rights reserved.
//

#import "Common.h"
#import <CommonCrypto/CommonDigest.h>
#import "SDImageCache.h"

@implementation Common
+ (NSString *) dateSwitchFromDate:(NSDate *)date{
	NSDate *now = [NSDate date];
	long interval = [now timeIntervalSinceDate:date];
	
	NSInteger minute = round(interval/60);
	NSInteger hour = round(interval/60/60);
	NSInteger day =  round(interval/60/60/24);
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *timeStr;
	if (minute+hour+day==0) {
		timeStr = @"刚刚";
	}else if (day>0) {
		timeStr = [dateFormatter stringFromDate:date];
	}else if (hour>0) {
		timeStr = [NSString stringWithFormat:@"%d小时前",hour];
	}else {
		timeStr = [NSString stringWithFormat:@"%d分钟前",minute];
	}
    [dateFormatter release];
    return timeStr;
}

+(NSDate *) nsDateFromDateString:(NSString *)dateString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	NSDate *date = [dateFormatter dateFromString:dateString];
    if (!date) {
        [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss ZZZ"];
        date = [dateFormatter dateFromString:dateString];
    }
    [dateFormatter release];
	return date;
}

+ (NSString *) keyWithURLString:(NSString *)url{
    // 取得URL的MD5值
    const char *str = [url UTF8String];
    unsigned char r[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, (CC_LONG)strlen(str), r);
    NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                          r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
    return filename;
}

+ (NSString *) dataFilePath:(NSString *)fileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *cPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:SAVEFILEPATH];
    if (fileName) {
        cPath = [cPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.data",fileName]];
    }
    
    return cPath;
}

+ (NSString *) cacheFilePath:(NSString *)fileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *cPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:SAVECACHEPATH];
    if (fileName) {
        cPath = [cPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.data",fileName]];
    }
    return cPath;
}

+(long long) cacheSize{
    SDImageCache *imageCache = [SDImageCache sharedImageCache];
    long long size = [imageCache cacheFileSize];
    
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    
    NSString *cPath = [Common dataFilePath:nil];
    
    NSArray* array = [fileManager contentsOfDirectoryAtPath:cPath error:nil];
    for(int i = 0; i<[array count]; i++){
        NSString *fullPath = [cPath stringByAppendingPathComponent:[array objectAtIndex:i]];
        
        BOOL isDir;
        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) ){
            NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
            size += fileAttributeDic.fileSize;
        }
    }
    
    cPath = [Common cacheFilePath:nil];
    
    array = [fileManager contentsOfDirectoryAtPath:cPath error:nil];
    for(int i = 0; i<[array count]; i++){
        NSString *fullPath = [cPath stringByAppendingPathComponent:[array objectAtIndex:i]];
        
        BOOL isDir;
        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) ){
            NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
            size += fileAttributeDic.fileSize;
        }
    }
    
    [fileManager release];
    return size;
}

+ (void) clearCache{
    // 计算缓存大小
    SDImageCache *imageCache = [SDImageCache sharedImageCache];
    [imageCache clearDisk];
    
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    
    NSString *cPath = [Common cacheFilePath:nil];
    
    NSArray* array = [fileManager contentsOfDirectoryAtPath:cPath error:nil];
    for(int i = 0; i<[array count]; i++){
        NSString *fullPath = [cPath stringByAppendingPathComponent:[array objectAtIndex:i]];
        
        BOOL isDir;
        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) ){
            [fileManager removeItemAtPath:fullPath error:nil];
        }
    }

    [fileManager release];
}
@end
